package com.haitaoit.chengquan.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.utils.NetworkMonitor;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class BaseActivity extends QBaseActivity implements OnClickListener {
    @ViewInject(R.id.left)
    private RelativeLayout left;
    @ViewInject(R.id.right)
    private RelativeLayout right;
    @ViewInject(R.id.title)
    private TextView       title;
    @ViewInject(R.id.content_layout)
    private LinearLayout   content_layout;

    /**
     * 设置标记栏左右标记,1是图片,0是文字,
     */
    private int tag = 0;
    private ProgressDialog mDialog;
    @Override
    public int getContentView() {
        return R.layout.activity_base;
    }

    @Override
    public void initView() {
        ViewUtils.inject(this);
        left = (RelativeLayout) findViewById(R.id.left);
        right = (RelativeLayout) findViewById(R.id.right);
        title = (TextView) findViewById(R.id.title);
        content_layout = (LinearLayout) findViewById(R.id.content_layout);

        mDialog = new ProgressDialog(this);
        mDialog.setCancelable(true);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    protected void updateUI(Message msg) {

    }

    /**
     * 设置子类的布局，传一个layoutID;如：R.layout.activity_base
     * setContentView_V(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param layoutID void
     * @throws
     * @since 1.0.0
     */
    public void setContentView_V(int layoutID) {
        // 获得inflater
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 把继承该BaseAcitivyt的layoutID放进来 显示
        View view = inflater.inflate(layoutID, null);
        // content_layout.addView(view);

        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        // content_layout.addView(view, layoutParams);
        view.setLayoutParams(layoutParams);
        if (null != content_layout) {
            content_layout.addView(view);
        }
    }

    /**
     * 获取内容显示区域的布局 content_layout getContentView_V(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     *
     * @return LinearLayout
     * @throws
     * @since 1.0.0
     */
    public LinearLayout getContentView_V() {
        return content_layout;
    }

    /**
     * 设置 Title的文字，传一个resID，如：R.string.home_title setTitle_V(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */

    public void setTitle_V(String msg) {
        title.setText(msg);
    }

    /**
     * 返回上一个Activity， backLeft_V(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param context
     * @param toClass void
     * @throws
     * @since 1.0.0
     */
    public void backLeft_V(final Context context, final Class<?> toClass) {
        left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, toClass);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 直接finish当前界面 backLeft_V(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) void
     *
     * @throws
     * @since 1.0.0
     */
    public void backLeft_V() {
        left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 默认显示左边按钮，如果设置为true,为显示，则反之 setLeftShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 –
     * 可选)
     *
     * @param states void
     * @throws
     * @since 1.0.0
     */
    public void setLeftShow(boolean states) {
        if (null == left) {
            return;
        }
        if (states) {
            left.setVisibility(View.VISIBLE);
        } else {
            left.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置左边按钮里是文字 setLeftShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    public void setLeftShow(int tag, int resID) {
        if (null == left) {
            return;
        }
        left.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = (TextView) view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            left.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            left.addView(view, params);
        }
    }

    /**
     * 默认显示右边按钮，如果设置为true,为显示，则反之 setRightShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 –
     * 可选)
     *
     * @param states void
     * @throws
     * @since 1.0.0
     */
    public void setRightShow(boolean states) {
        if (null == right) {
            return;
        }
        if (states) {
            right.setVisibility(View.VISIBLE);
        } else {
            right.setVisibility(View.INVISIBLE);
        }
    }

    public void setRightShow(int tag, int resID) {
        if (null == right) {
            return;
        }
        right.removeAllViews();
        right.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = (TextView) view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            right.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            right.addView(view, params);
        }
    }


    /**
     * 获取 left , 左边按钮 getLift_V(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @return RelativeLayout
     * @throws
     * @since 1.0.0
     */
    public RelativeLayout getLift_V() {
        if (left == null) {
            left = (RelativeLayout) findViewById(R.id.left);
            return left;
        }
        return left;
    }

    /**
     * 获取 right , 右边按钮 getRight_V(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @return RelativeLayout
     * @throws
     * @since 1.0.0
     */
    public RelativeLayout getRight_V() {
        if (right == null) {
            right = (RelativeLayout) findViewById(R.id.right);
            return right;
        }
        return right;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View v) {
    }

    public void showProgrees(String text) {
        if (TextUtils.isEmpty(text)) {
            mDialog.setMessage("加载中,请稍等.....");
        } else {
            mDialog.setMessage(text);
        }
        mDialog.show();
    }

    public void hideProgree() {
        mDialog.dismiss();
    }
    public boolean hasNetWork(){
        boolean connected = NetworkMonitor.isConnected(this);
        if(!connected){
            ToastUtils.showToast("无网络连接,请稍后再试");
        }
        return connected;
    }

}
